<?php

namespace App\Http\Controllers\Api;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function login(Request $request){
        $user = User::where($request->all())->first();
        if ($user){
            $user->token = md5($user->username);
            $user->save();
            return response()->json(['role'=>$user->role,'token'=>$user->token],200);
        }else{
            return response()->json('invalid login',401);
        }
    }

    public function logout(Request $request)
    {
        $user = User::whereToken($request->token)->first();
        if ($user){
            $user->token = null;
            $user->save();
            return response()->json('logout success',200);
        }else{
            return response()->json('Unauthorized user',200);
        }
    }
}
